// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2018-2020.
 * Description: Disable schedule when dump stack info
 * Author: lalihangliang
 * Create: 2023-10-11
 */
#include <linux/smp.h>
#include <linux/sched.h>

#include <linux/processor.h>
#include <linux/compiler.h>
#include <linux/euleros_dump.h>

#ifdef CONFIG_EULEROS_DUMP
/* stop other CPUs when save data */
#ifdef CONFIG_ARM64
volatile unsigned long dump_oncpu;
EXPORT_SYMBOL(dump_oncpu);
#elif defined CONFIG_X86
volatile unsigned long cpus_sched_wait;
EXPORT_SYMBOL(cpus_sched_wait);
#endif

void wait_on_dump_stack(void)
{
#ifdef CONFIG_ARM64
	volatile unsigned long *dump_cpu = &dump_oncpu;
#elif defined CONFIG_X86
	volatile unsigned long *dump_cpu = &cpus_sched_wait;
#endif
	/* if we are saving the stack info, schedule()
	 * is a no-op for the dumping cpu, and all
	 * other cpus are forced to wait until the dump
	 * completes.
	 */

	if (unlikely(*dump_cpu)) {
		if (*dump_cpu == smp_processor_id() + 1)
			return;
		while (*dump_cpu)
			cpu_relax();
	}
}

int euleros_get_dump_flag(void)
{
#ifdef CONFIG_ARM64
	return dump_oncpu && (dump_oncpu != smp_processor_id() + 1);
#elif defined CONFIG_X86
	return cpus_sched_wait && (cpus_sched_wait != smp_processor_id() + 1);
#endif
}

#endif
